<?php
		
	namespace app\admin\controller;

	use \think\Db;

	class Hotel extends Base{

		public function index(){

			try{

				if($this->method=='post'){

					$page=input('post.page/d');

					$pagesize=10;

					$count=Db::name('hotel')->count();

					$allpage=ceil($count/$pagesize);

					$page=$page>$allpage?$allpage:$page;

					$page=$page>=1?$page:1;

					$start=($page-1)*$pagesize;

					$row=Db::name('hotel')->order('h_id desc')->limit($start,$pagesize)->select();

					return $this->success(['data'=>$row,'page'=>$page,'pagesize'=>$pagesize,'allpage'=>$allpage,'count'=>$count]);

				}else{

					echo '拒绝访问';

				}

			}catch(\Exception $e){

				return $this->err($e);

			}

		}

		public function edit(){

			try{

				if($this->method=='post'){

					$id=(int)input('post.id');

					$row=Db::name('hotel')->where(['h_id'=>$id])->find();

					if($row){

						return $this->success($row);

					}

					return $this->fail('该酒店不存在~');

				}else{

					echo '拒绝访问';

				}

			}catch(\Exception $e){

				return $this->err($e);

			}

		}

		public function edit_save(){

			try{

				if($this->method=='post'){

					$id=(int)input('post.h_id');

					$data['h_name']=input('post.h_name');

					if($data['h_name']==''){

						return $this->fail('请输入酒店名称~');

					}

					$data['h_icon']=input('post.h_icon');

					$file=$this->clearIcon($data['h_icon']);

					if(!is_file(ROOTPATH.$file)){

						return $this->fail('请上传酒店图片~');

					}

					$data['h_money']=(int)input('post.h_money');

					if(!$data['h_money']){

						return $this->fail('请输入酒店金额~');

					}

					$data['h_areaId']=input('post.h_areaId/a');

					if($data['h_areaId']=='' || count($data['h_areaId'])<3){

						return $this->fail('请选择酒店地址~');

					}

					$data['h_areaText']=input('post.h_areaText/a');

					if($data['h_areaText'][1]=='市辖区' || $data['h_areaText'][1]=='县'){
						$data['h_areaText'][1]=$data['h_areaText'][0];
					}

					$data['h_areaText'][2]=str_replace("县","区",$data['h_areaText'][2]);

					$data['h_qu']=$data['h_areaText'][2];

					$data['h_address']=input('post.h_address');

					if($data['h_address']==''){

						return $this->fail('请输入酒店详细地址~');

					}

					$data['h_room']=input('post.h_room/a');

					$row=Db::name('hotel')->where(['h_id'=>$id])->find();

					$file2=str_replace('temp','uploads',$file);

					$dir=ROOTPATH.'/uploads';

					if(!is_dir($dir)){
						mkdir($dir);
					}

					$dir.='/'.date('Ymd');

					if(!is_dir($dir)){
						mkdir($dir);
					}

					if($file2!=$file){

						if(@copy('.'.$file,'.'.$file2)){

							@unlink('.'.$file);

							$data['h_icon']='//'.$_SERVER['HTTP_HOST'].$file2;

						}

					}

					if($row){

						$res=Db::name('hotel')->where(['h_id'=>$id])->update($data);

						if($row['h_icon']!=$data['h_icon']){

							@unlink('.'.$this->clearIcon($row['h_icon']));

						}

					}else{

						$id=Db::name('primary')->where(['type'=>'hotel'])->find();

						if(!$id){

							$data['h_id']=1;

							Db::name('primary')->insert(['type'=>'hotel','id'=>2]);

						}else{

							$data['h_id']=$id['id'];

							Db::name('primary')->where(['type'=>'hotel'])->setInc('id',1);

						}

						$res=Db::name('hotel')->insert($data);


					}

					return $this->success();


				}else{

					echo '拒绝访问';
						
				}

			}catch(\Exception $e){

				return $this->err($e);

			}

		}

		public function dele(){

			try{

				if($this->method=='post'){

					$id=(int)input('post.id');

					$row=Db::name('hotel')->where(['h_id'=>$id])->find();

					if(!$row){

						return $this->fail('该酒店不存在~');

					}

					$res=Db::name('hotel')->where(['h_id'=>$id])->delete();

					if($res){

						

						@unlink('.'.$this->clearIcon($row['h_icon']));

						return $this->success();

					}

					return $this->fail('删除失败~');
				}else{

					echo '拒绝访问';

				}

			}catch(\Exception $e){

				return $this->err($e);

			}

		}

	}

	/*
	h_name: '',
                h_icon: '',
                h_money: '',
                h_address: '',
                h_areaId:[],
                h_areaText:[]
	 */

?>